package mq

import (
	"fmt"
)

type Event interface {
	Run()
}

type Message struct {
	Content interface{}
}

type DefaultEvent struct {
	message *Message
}

func (d DefaultEvent) Run() {
	fmt.Printf("Message Content is : %s\n", d.message.Content)
}

type EventBus struct {
	// 事件通道
	channel  chan Event
	capacity uint32
	size     uint32
	counts   uint32
}

func NewEventBus(size uint32) *EventBus {
	return &EventBus{
		channel:  make(chan Event, size),
		capacity: size << 1,
		size:     size,
		counts:   0,
	}
}

// 发布一个事件
func (eb *EventBus) Post(event Event) {
	eb.channel <- event
	eb.counts++
}

func (eb *EventBus) Register(sub *Subscriber) {
	go sub.Work(eb.channel)
}
